/* ---------------------------------------------------------------------------------------------
 *
 *            Capital Alliance Software Confidential Proprietary
 *            (c) Copyright CAS 201{x}, All Rights Reserved
 *                          www.pekall.com
 *
 * ----------------------------------------------------------------------------------------------
 */
package com.elvis.music.util;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Environment;
import android.util.Log;
/**
 * 文件操作
 * @author LeiKang
 */
public class FileOperate {

    private static final String TAG = FileOperate.class.getCanonicalName();

    /** sdCard文件夹 */
    private static final File mSdcardFolder = Environment.getExternalStorageDirectory();

    /** sdCard文件夹路径 */
    public static final String path = mSdcardFolder.getParent() + "/" + mSdcardFolder.getName();

    /**
     * 判断是否存在SD卡
     * 
     * @return
     */
    public boolean isSDCard() {
        return Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState());
    }

    /**
     * SD卡新建文件夹 ps：传入参数为文件夹名称，只能在SDCard根目录下新建
     */
    public void addFolderToSDCard(String folderName) {
        String fileDirectory = path + java.io.File.separator + folderName;// 文件夹目录
        File folder = new File(fileDirectory);
        // 不存在，创建
        if (!folder.exists())
            folder.mkdirs();
    }

    public void addFileToSDCard(String filePath, String result) throws IOException {
        String fileDirectory = filePath.substring(0,filePath.indexOf(".")) + ".lrc";// 文件夹目录
        File folder = new File(filePath.substring(0,filePath.lastIndexOf("/")+1));
        
        if (!folder.exists())
            folder.mkdirs();
        File file = new File(fileDirectory);
        if(!file.exists()){
            file.createNewFile();
            FileWriter fw = new FileWriter(file);
            fw.write(result);
            fw.close();
        }
    }

    /**
     * 重命名SD卡文件夹 ps：传入参数为需要修改，修改后的文件夹名称，只能操作于SDCard根目录
     */
    public void renameFolderOfSDCard(String oldFolderName, String newFolderName) {
        String fileDirectory = path + java.io.File.separator + oldFolderName;
        File oldFolder = new File(fileDirectory);
        fileDirectory = path + java.io.File.separator + newFolderName;
        File newFolder = new File(fileDirectory);
        if (oldFolder.exists())
            oldFolder.renameTo(newFolder);
    }

    /**
     * 删除SD卡中的某一文件夹 ps：删除SD根目录下某一文件夹
     */
    public void deleteFolderOfSDCard(String folderName) {
        String fileDirectory = path + java.io.File.separator + folderName;// 文件夹目录
        File folder = new File(fileDirectory);
        if (folder.exists())
            folder.delete();
    }

    /**
     * 将一个文件放入SD中的某一文件夹
     */
    // 未实现
    public void putFileToSDCard(File file, String folderName) {

        String fileDirectory = path + java.io.File.separator + folderName;
        Log.d(TAG, fileDirectory);
        File sdFile = new File(file, fileDirectory);
        try {
            sdFile.createNewFile();
        } catch (IOException e) {
            Log.d(TAG, "[putFileToSdCard]---", e);
            e.printStackTrace();
        }
    }

    /**
     * 获取某一文件夹下的所有文件名和文件夹路径 ps：如果返回文件夹，需要其他操作 这里只返回该文件夹的 一级子目录
     */
    public String[] getAllFilesOfFolder(String folderName) {
        String fileDirectory = path + java.io.File.separator + folderName;
        File folder = new File(fileDirectory);
        if (folder.isDirectory()) {
            String[] fileList = folder.list();
            for (int i = 0; i < fileList.length; i++)
                fileList[i] = fileDirectory + "/" + fileList[i];
            return fileList;
        }
        return null;
    }

    /**
     * 判断传入某一路径是文件还是文件夹
     * 
     * @param path
     * @return
     */
    public boolean isDirectory(String path) {
        File folder = new File(path);
        if (folder.isFile())
            return true;
        return false;
    }

    /**
     * 根据sd卡中的路径获取图片view 如/mnt/sdcard/pic/abc.png 这里返回的是Bitmap格式
     * 需要设置ImageView的：ImageView.serImageBitmap(bm)
     */
    public Bitmap getImageByPath(String filePath, int type) {
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inSampleSize = 2;
        Bitmap bm = BitmapFactory.decodeFile(filePath, options);
        return bm;
    }

    private List<String> mfiles = new ArrayList<String>();

    /**
     * 获取SD卡中某文件夹的所有文件的位置（不包含文件夹） indexOf() 可以条件筛选 ps：.mp4 , .bmp , .text
     * 
     * @param p 初始传入的为文件夹路径
     */
    public void getTypeFiles(String p) {

        File folder = new File(p);
        String[] folders = folder.list();
        /* =====================判断文件夹下是否为空============== */
        if (folders == null)
            return;
        /* ==================================================== */
        for (int i = 0; i < folders.length; i++) {

            File subFile = new File(p + "/" + folders[i]);

            if (subFile.isDirectory()) {
                getTypeFiles(p + "/" + folders[i]);
            }
            if (subFile.isFile()) {
                mfiles.add(p + "/" + folders[i]);
            }
        }
    }

    public List<String> getMfiles() {
        return mfiles;
    }

    public void setMfiles(List<String> mfiles) {
        this.mfiles = mfiles;
    }

}
